package com.lovesher.payment.service.service.impl;

import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.lovesher.payment.common.resposity.bean.AccountRechargeDTO;
import com.lovesher.payment.common.resposity.bean.PaymentSerial;
import com.lovesher.payment.common.resposity.bean.TransferDTO;
import com.lovesher.payment.common.resposity.bean.TransferResultDTO;
import com.lovesher.payment.common.service.PaymentService;
import com.lovesher.payment.service.resposity.bean.PaymentInterface;
import com.lovesher.payment.service.resposity.dao.PaymentSerialDao;
import com.lovesher.payment.service.resposity.dao.PluginConfigDao;

@Component("paymentService")
public class PaymentServiceImpl implements PaymentService{
	
	private Logger logger = LoggerFactory.getLogger(PaymentServiceImpl.class);
	
	@Resource
	private PaymentSerialDao paymentSerialDao;
	
	@Resource
	private PluginConfigDao pluginConfigDao;
	
	
	@Override
	public Map<String,String> doPay(AccountRechargeDTO accountRechargeDTO) {
		return paymentSerialDao.sendPayReq(accountRechargeDTO);
	}


	@Override
	public String getPaymentName(String payment) {
		PaymentInterface paymentInterface = this.pluginConfigDao.getPaymentPlugin(payment);
		if(paymentInterface != null){
			return paymentInterface.getName();
		}else{
			return "";
		}
	}

	
	@Override
	public Boolean queryOrderResultByPluginId(PaymentSerial paymentSerial) {
		Boolean result = false;
		try {
			PaymentInterface paymentInterface = this.pluginConfigDao.getPaymentPlugin(paymentSerial.getPaymentMethod());
			if (null == paymentSerial || null == paymentInterface) {
				logger.error("queryOrderResultByPluginId get info failed! paymentSerial ID:"+paymentSerial.getId());
				return result;
			}
			result = paymentInterface.queryOrderResult(paymentSerial);
		} catch (Exception e) {
			logger.error("queryOrderResultByPluginId error! paymentSerial:"+paymentSerial.getId(), e);
			throw new RuntimeException("queryOrderResultByPluginId error! paymentSerial:"+paymentSerial.getId(), e);
		}
		return result;
	}


	@Override
	public TransferResultDTO doTransfer(TransferDTO transferDTO) {
		return paymentSerialDao.doTransfer(transferDTO);
	}

}
